package com.sitech.ibnms.util.charts.fusion.cylinder;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sitech.ibnms.util.charts.domain.HIS_DATA_DOMAIN;
import com.sitech.ibnms.util.charts.fusion.FusionChartCommon;

public class FusionCylinderChart {

	public FusionCylinderChart() {
	}

	public static void main(String[] args) {
		FusionCylinderChart chart = new FusionCylinderChart();
		FusionCylinderChartParams params = new FusionCylinderChartParams();
		params.setDiv_id("div_id");
		params.setChartPath("Cylinder.swf");
		params.setChartWidth("150");
		params.setChartHeight("200");
		params.setChartId("myChartId");
		HIS_DATA_DOMAIN his_data_domain = new HIS_DATA_DOMAIN();
		String html = chart.getCylinderHtml(params, his_data_domain, null);
		System.out.println(html);

	}

	public String getCylinderHtml(FusionCylinderChartParams params,
			HIS_DATA_DOMAIN his_data_domain, List annotationsList) {
		String xmlDate = getXmlDate(params, his_data_domain, annotationsList);
		return FusionChartCommon.getScript(params)
				.replaceAll("######", xmlDate);
	}

	public String getXmlDate(FusionCylinderChartParams params,
			HIS_DATA_DOMAIN his_data_domain, List annotationsList) {
		String kpi_value = "";
		if (his_data_domain != null) {
			kpi_value = his_data_domain.getKPI_VALUE3();
			kpi_value = kpi_value != null ? kpi_value : "";
		}

		Document document = DocumentHelper.createDocument();
		try {
			Element chart = document.addElement("chart");
			String headAttribute[] = FusionCylinderChartParamsName.paramName;
			String headAttributeValue[] = FusionCylinderChartParams.getParamVaule(params,
					headAttribute);
			int idx = 0;
			for (String name : headAttribute) {
				chart.addAttribute(name, headAttributeValue[idx]);
				idx++;
			}
			Element value = chart.addElement("value");
			value.setText(kpi_value);
			if (annotationsList != null && annotationsList.size() > 0) {
				Element annotations = chart.addElement("annotations");
				Element annotationGroup = annotations
						.addElement("annotationGroup");
				annotationGroup.addAttribute("showBelowChart", "1");
				for (int i = 0; i < annotationsList.size(); i++) {
					AnnotationsParams annotationsParams = (AnnotationsParams) annotationsList
							.get(i);
					setAnnotations(annotationGroup, annotationsParams);
				}
			}
		} catch (Exception ex) {
			System.out
					.println("add root chart has exception ,method : setLineChartParameter();");
			ex.printStackTrace();
		}
		return formatXMLData(document);

	}

	private void setAnnotations(Element annotationGroup,
			AnnotationsParams annotationsParams) {
		Element annotation = annotationGroup.addElement("annotation");
		String headAttribute[] = AnnotationsParamsNames.paramName;
		String headAttributeValue[] = annotationsParams.getParamVaule(
				annotationsParams, headAttribute);
		int idx = 0;
		for (String name : headAttribute) {
			annotation.addAttribute(name, headAttributeValue[idx]);
			idx++;
		}

	}

	private String formatXMLData(Document document) {
		String xml[] = document.asXML().split("\n");
		return xml[1].replace("\"", "\'");
	}
}
